package com.group15.phms;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MedicationMonitor extends BroadcastReceiver{
	//creates an alarm for the time given
	public boolean createNotification(Medication med, Dose dose, Context context){
		/* 
		 */
		boolean added = false;
		
		long nextDay = 24*60*60*1000; //24 hours in milliseconds
		Calendar today = Calendar.getInstance();
		int day = today.get(Calendar.DAY_OF_MONTH);
		int month = today.get(Calendar.MONTH);
		int year = today.get(Calendar.YEAR);
		//set (int year, int month, int day, int hourOfDay, int minute, int second)
		today.set(year, month, day, dose.getHour(), dose.getMinute(), 0);
		int id = dose.getHour() + dose.getMinute();//used to id the different alarms
		
		//getTimeInMillis ()
		long alarmTime = today.getTimeInMillis();

        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(context.getApplicationContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, nextDay, alarmIntent);
        	added = true; 
        
        return added;
	}//end of newNotification()
	
	public boolean createTempNotification(Medication med, long alarmTime, int alarmNumber, Context context){
		/* Used for creating a second medication alarm, if user "snoozes" the previous one
		 * 		new alarm is set for 10 minutes later.
		 */
		boolean added = false;

		int id = 0;
		long diff = alarmNumber * 10 * 60 * 1000; //add 10 minutes to previous alarm (if this is alarm #2, and original alarm is a 1200 -> 1210

		alarmTime += diff; //alarm time in milliseconds + 10 minutes

        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(context.getApplicationContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, alarmTime, alarmIntent);
        	added = true; 
        
        return added;
	}//end of newNotification()
	
	public boolean deleteNotification(Medication med, Dose dose, Context context){
		boolean deleted = false;
		//will need some way of identifying different alarms i.e. medication ID
		int id = dose.getHour() + dose.getMinute();
		
		//Intent intent = new Intent(context, AlarmReceiver.class);
		Intent intent = new Intent(context, AlarmReceiverActivity.class);
		PendingIntent alarmIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		am.cancel( alarmIntent );
		Toast toast = Toast.makeText(context, "Alarm deleted.", Toast.LENGTH_SHORT);
		toast.show();
		deleted = true;
		
		return deleted;
	}//end of deleteNotification()

	@Override
	public void onReceive(Context context, Intent intent) {
		//start activity
				Intent intent2 = new Intent(context, AlarmReceiverActivity.class); 
		        intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		        context.startActivity(intent2);		
	}
}
